package com.pfzy.ui.activity.nest_recycler

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.pfzy.ui.activity.nest_recycler.adapter.MainAdapter
import com.pfzy.ui.activity.nest_recycler.adapter.SubAdapter
import com.pfzy.ui.activity.nest_recycler.adapter.TabAdapter
import com.pfzy.ui.activity.nest_recycler.bean.MainListBean
import com.pfzy.ui.activity.nest_recycler.bean.MainTabBean
import com.pfzy.ui.databinding.ActivityNestRecyclerBinding

class NestRecyclerViewActivity: AppCompatActivity()  {
    private lateinit var mBind: ActivityNestRecyclerBinding
    private lateinit var mainAdapter: MainAdapter
    private lateinit var tabAdapter: TabAdapter
    private lateinit var subAdapters: MutableList<SubAdapter>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = ActivityNestRecyclerBinding.inflate(layoutInflater)
        setContentView(mBind.root)
        initView()
        loadData()
        initEvent()
    }

    private fun initView() {
        mainAdapter = MainAdapter()
        mBind.recyclerView.itemAnimator = null
        mBind.recyclerView.adapter = mainAdapter
    }

    /**
     * 加载数据
     */
    private fun loadData() {
        tabAdapter = TabAdapter()
        subAdapters = arrayListOf(
            SubAdapter(),
            SubAdapter(),
            SubAdapter(),
            SubAdapter()
        )

        val list = ArrayList<MainListBean>()
        for (i in 1..10)
            list.add(MainListBean(MainListBean.TYPE_MAIN, "${i}"))

        val tabs = ArrayList<MainTabBean>(4)
        for (i in 1..4)
            tabs.add(MainTabBean("Tab${i}", i == 1))
        tabAdapter.setList(tabs)

        subAdapters.forEach {
            val list = ArrayList<String>()
            for (i in 1..12)
                list.add("${i}")
            it.setList(list)
        }

        list.add(MainListBean(MainListBean.TYPE_SUB, tabAdapter, subAdapters))

        mainAdapter.setList(list)
    }

    /**
     * 初始化触发事件
     */
    private fun initEvent() {
        mainAdapter.setOnChildScrollEndListener { recyclerView, newState ->
            Log.d("滚动视图1","滚动子RY到底部了")
            // 这里可以加载更多数据
        }
        mainAdapter.setTabSelectListener { viewPager, position ->
            tabAdapter.setSelectAt(position)
            viewPager.setCurrentItem(position, false)
        }
        mainAdapter.setOnTurnPageListener { tabRecyclerView, position ->
            tabAdapter.setSelectAt(position)
        }
    }
}